﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Fusion.Interface
{
	/// <summary> Class for repeated button presses </summary>
	public class RepeatButton
	{
		public event VoidDelegate Press;
		public event VoidDelegate Release;

		public bool state;
		float timer;
		public float threshhold;
		public float repeat;

		public RepeatButton()
		{
			state = false;
			timer = 0;
			threshhold = 32;
			repeat = 8;
		}

		public RepeatButton( int thresh, int rep, VoidDelegate down )
		{
			state = false;
			timer = 0;
			threshhold = thresh;
			repeat = rep;
			Press += down;
		}

		public RepeatButton( int thresh, int rep, VoidDelegate down, VoidDelegate up )
		{
			state = false;
			timer = 0;
			threshhold = thresh;
			repeat = rep;
			Press += down;
			Release += up;
		}

		public void Update( float period, bool buttonState )
		{
			if( buttonState == false )
			{
				if( state == true && Release != null )
					Release();

				if( timer > 0 )
					timer = 0;
				else
					timer--;
			}
			else
			{
				if( state == false )
					Press();

				if( timer < 0 )
					timer = 0;
				else
					timer++;

				if( timer == threshhold && Press != null )
					Press();

				if( timer >= threshhold + repeat )
				{
					if( Press != null )
						Press();
					timer = threshhold;
				}
			}

			state = buttonState;
		}
	}
}